package deibay.server.io;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import org.jdom.Element;
import deibay.datamodel.Item;

public class XmlProduct {

	private Element product = null;
	SimpleDateFormat dateFormat = null;
	
	public XmlProduct(Item p)
	{
		dateFormat= new SimpleDateFormat("d MM yyyy HH:mm:ss");
		product = new Element("Product");
		Element eName = new Element("name");
		eName.addContent(p.getName());
		Element eDescr = new Element("description");
		eDescr.addContent(p.getDescription());
		Element lSeller = new Element("login_seller");
		lSeller.addContent(p.getSeller());
		Element lBidder = new Element("login_bidder");
		lBidder.addContent(p.getBidder());
		Element lSubscribedUsers = new Element("subscribed_users");
		for(String user: p.getSubscribedUsers())
		{
			Element el = new Element("user");
			el.addContent(user);
			lSubscribedUsers.addContent(el);
		}
		Element id = new Element("id");
		id.addContent(p.getId());
		Element eCat = new Element("category");
		eCat.addContent(p.getCategory().toString());
		Element ePrice = new Element("startprice");			
		ePrice.addContent(""+p.getStartPrice());
		Element aPrice = new Element("actualprice");			
		aPrice.addContent(""+p.getActualPrice());
		Element eDate = new Element("finish_date");		
		eDate.addContent(""+dateFormat.format(p.getFinishTime()));
		product.addContent(eName);
		product.addContent(lBidder);
		product.addContent(lSeller);
		product.addContent(lSubscribedUsers);
		product.addContent(eDescr);
		product.addContent(id);
		product.addContent(eCat);
		product.addContent(ePrice);	
		product.addContent(aPrice);
		product.addContent(eDate);
	}
	
	public XmlProduct(Element product)
	{
		dateFormat= new SimpleDateFormat("d MM yyyy HH:mm:ss");
		this.product = product;
	}
	
	public Item xmlToObject()
	{
		Item p = null;
		
		List<Element> elements = product.getChildren();
		String name = "", description = "", loginSeller = "", loginBidder = "", id = "", category ="";
		int sprice = 0, aprice = 0;
		Date d = null;
		Vector<String> users = new Vector<String>();
		
		for(Element e: elements)
		{
			if(e.getName().equals("name"))
			{
				name = e.getValue();
			}
			else if(e.getName().equals("login_bidder"))
			{
				loginBidder = e.getValue();
			}
			else if(e.getName().equals("login_seller"))
			{
				loginSeller = e.getValue();
			}
			else if(e.getName().equals("subscribed_users"))
			{
				List<Element> subscribed = e.getChildren();
				for(Element el: subscribed)
					users.add(el.getValue());					
			}
			else if(e.getName().equals("description"))
			{
				description = e.getValue();
			}
			else if(e.getName().equals("id"))
			{
				id = e.getValue();
			}
			else if(e.getName().equals("category"))
			{
				category = e.getValue();
			}
			else if(e.getName().equals("startprice"))
			{
				sprice = Integer.parseInt(e.getValue());
			}
			else if(e.getName().equals("actualprice"))
			{
				aprice = Integer.parseInt(e.getValue());
			}
			else if(e.getName().equals("finish_date"))
			{							
				try {
					d = dateFormat.parse(e.getValue());
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}			
			}
		}
		
		p = new Item(name, description, loginSeller, loginBidder, users, id, category, sprice, aprice, d);
		return p;
	}
	
	public Element getXmlProduct()
	{
		return this.product;
	}
}
